﻿package com.dlxj.action;

import java.util.ArrayList;
import java.util.List;

import com.dlxj.entity.Circuitry;
import com.dlxj.entity.CircuitryPole;
import com.dlxj.entity.Pole;
import com.dlxj.service.CircuitryService;
import com.dlxj.util.MyJSON;
import com.dlxj.util.PageBean;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class CircuitryAction extends ActionSupport {

	private CircuitryService circuitryService;
	private String json;
	private String cid; // 路线ID
	private String pid; // 杆塔ID
	private String cname;
	private Circuitry circuitry;
	private String poleIds;
	private List<CircuitryPole> circuitryPoles;
	int pageIndex;
	int pageSize;
	
	
	public int getPageIndex() {
		return pageIndex;
	}


	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public String findPoleByStartpoleNo(){
		List<Pole> lists=new ArrayList<Pole>();
		System.out.println("----------name:"+cname);
		lists = circuitryService.findPoleByStartpoleNo(cname);
		/*Pole pole=new Pole();*/
		/*for(int i=0;i<lists.size();i++){
			pole=lists.get(index);
		}*/
		json = MyJSON.toJson(lists, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}
	
	
	public String findStartPoleByCName(){
		String name=circuitry.getName();
	    Circuitry circuitry=circuitryService.findStartPoleByCName(name);
		json = MyJSON.toJson(circuitry).toString();
		return "success";
	}
	
	
	public String findList() {

		List<Circuitry> circuitries = circuitryService.findCircuitryList();

		json = MyJSON.toJson(circuitries, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();

		return "success";
	}

	public String findPoliesByCid() {

		json = MyJSON.toJson(circuitryService.findPoliesByCid(cid),
				"circuitriesForStartpoleNo", "circuitriesForEndpoleNo")
				.toString();

		return "success";
	}

	public String findAllPolies() {

		json = MyJSON.toJson(circuitryService.findAllPolies(),
				"circuitriesForStartpoleNo", "circuitriesForEndpoleNo")
				.toString();

		return "success";
	}

	public String deleteRoutLine() {
		int code = circuitryService.deleteRoutLine(cid);
		json = "{code:" + code + "}";
		return "success";
	}

	public String addCircuitry() {
		setPoleIds(poleIds);
		int code = circuitryService.addCircuitry(circuitry, circuitryPoles);
		json = "{code:" + code + "}";

		return "success";
	}

	public String findByPid() {
		List<Circuitry> circuitries = circuitryService
				.findCircuitriesByPid(pid);

		json = MyJSON.toJson(circuitries, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();

		return "success";

	}

	public CircuitryService getCircuitryService() {

		return circuitryService;

	}

	public void setCircuitryService(CircuitryService circuitryService) {

		this.circuitryService = circuitryService;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getCid() {
		return cid;
	}

	public String getCname() {
		return cname;
	}


	public void setCname(String cname) {
		this.cname = cname;
	}


	public void setCid(String cid) {
		this.cid = cid;
	}

	public Circuitry getCircuitry() {
		return circuitry;
	}

	public void setCircuitry(Circuitry circuitry) {
		this.circuitry = circuitry;
	}

	public String getPoleIds() {
		return poleIds;
	}

	public void setPoleIds(String poleIds) {
		this.poleIds = poleIds;
		String[] ids = poleIds.split(",");
		if (ids != null) {
			circuitryPoles = new ArrayList<CircuitryPole>();
			for (int i = 0; i < ids.length - 1; i++) {
				CircuitryPole cp = new CircuitryPole();
				cp.setPid(ids[i]);
				cp.setPosition(i + 1);
				circuitryPoles.add(cp);
			}
			CircuitryPole cp = new CircuitryPole();
			cp.setPid(ids[ids.length - 1]);
			cp.setPosition(99999);
			circuitryPoles.add(cp);
		}
	}

	public List<CircuitryPole> getCircuitryPoles() {
		return circuitryPoles;
	}

	public void setCircuitryPoles(List<CircuitryPole> circuitryPoles) {
		this.circuitryPoles = circuitryPoles;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	/**
	 * 苏子桑加
	 * @return
	 */
	public String findcircuitry(){
		if(circuitry.getPoleByEndpoleNo()!=null){
			circuitry.setPoleByStartpoleNo(circuitry.getPoleByEndpoleNo());
			System.out.println("--------------------");
		}
		List<Circuitry> circuitryList=circuitryService.findcircuitry(circuitry);
		System.out.println(circuitryList.size());
		json = MyJSON.toJson(circuitryList,"circuitriesForStartpoleNo","circuitriesForEndpoleNo").toString();
		System.out.println("this is json:"+json);
		return "success";
	}
	
	/**
	 * zkl加 分页
	 * @return
	 */
	public String findbypage(){
		if(circuitry.getPoleByEndpoleNo()!=null){
			circuitry.setPoleByStartpoleNo(circuitry.getPoleByEndpoleNo());
			System.out.println("--------------------");
		}
		PageBean bean =  circuitryService.findcircuitrybyPage(circuitry, pageIndex, pageSize);
		json = MyJSON.toJson(bean,"circuitriesForStartpoleNo","circuitriesForEndpoleNo").toString();
		return "success";
	}
	
	
}
